package com.example.cofeed.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import com.example.cofeed.R;
import com.example.cofeed.activity.WebViewActivity;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.entity.IndexNumber;
import com.gongwen.marqueen.MarqueeFactory;

public class ComplexViewMF extends MarqueeFactory<View, IndexNumber> {
    private LayoutInflater inflater;

    public ComplexViewMF(Context mContext) {
        super(mContext);
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public View generateMarqueeItemView(IndexNumber data) {
        View mView = (View) inflater.inflate(R.layout.adapter_index_number, null);
        ((TextView) mView.findViewById(R.id.tv_title)).setText(data.title);
        ((TextView) mView.findViewById(R.id.tv_unit)).setText(data.zhishu + data.unit);
        ((TextView) mView.findViewById(R.id.tv_zhangdie)).setText(data.zhangdie + "");
        if (data.zhangdie > 0) {
            ((ImageView) mView.findViewById(R.id.iv3)).setImageDrawable(mContext.getResources().getDrawable(R.mipmap.downtop));
            ((TextView) mView.findViewById(R.id.tv_unit)).setTextColor(mContext.getResources().getColor(R.color.colorLightRed));
            ((TextView) mView.findViewById(R.id.tv_zhangdie)).setTextColor(mContext.getResources().getColor(R.color.colorLightRed));

        } else if (data.zhangdie < 0) {
            ((ImageView) mView.findViewById(R.id.iv3)).setImageDrawable(mContext.getResources().getDrawable(R.mipmap.arrowsdown));
            ((TextView) mView.findViewById(R.id.tv_unit)).setTextColor(mContext.getResources().getColor(R.color.colorGreen));
            ((TextView) mView.findViewById(R.id.tv_zhangdie)).setTextColor(mContext.getResources().getColor(R.color.colorGreen));

        } else {
            ((ImageView) mView.findViewById(R.id.iv3)).setImageDrawable(mContext.getResources().getDrawable(R.mipmap.minus));
            ((TextView) mView.findViewById(R.id.tv_unit)).setTextColor(mContext.getResources().getColor(R.color.colorBlue));
            ((TextView) mView.findViewById(R.id.tv_zhangdie)).setTextColor(mContext.getResources().getColor(R.color.colorBlue));
        }
        mView.setOnClickListener(v -> {
            if (ObjectUtils.isNotEmpty(data.url)){
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(IntentKeyConstant.ID, data.id);
                intent.putExtra(IntentKeyConstant.URL, data.url);
                mContext.startActivity(intent);
            }
        });
        return mView;
    }
}